﻿
namespace DAL
{       
    using System;
    using System.ComponentModel.Composition;
    using Entities;
	
    [Export(typeof(IUnitOfWork)), PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class UnitOfWork : IUnitOfWork, IDisposable
    {
        #region Private Variables
        
        private FAEntities context = null;
        private bool disposed = false;
      
        private IBotsMachineRepository _botsmachineRepository;
      
        private IBotsUserRepository _botsuserRepository;
      
        private IBotRecipientRepository _botrecipientRepository;
      
        private IBotPeriodHourRepository _botperiodhourRepository;
      
        private IBotsOperationRepository _botsoperationRepository;
      
        private IOperationRepository _operationRepository;
      
        private IBOTRepository _botRepository;
      
        private IUserRepository _userRepository;
      
        private IAuditedOperationRepository _auditedoperationRepository;

        #endregion        
        
        #region Constructors

        [ImportingConstructor]
        public UnitOfWork()
        {
            this.context = new FAEntities();
        }

        #endregion

        #region IUnitOfWork
 
        public IBotsMachineRepository BotsMachineRepository
        {
            get
            {
                if (this._botsmachineRepository == null)
                {
                    this._botsmachineRepository = new BotsMachineRepository(this.context);
                }
                
                return this._botsmachineRepository; 
            }
        }
 
        public IBotsUserRepository BotsUserRepository
        {
            get
            {
                if (this._botsuserRepository == null)
                {
                    this._botsuserRepository = new BotsUserRepository(this.context);
                }
                
                return this._botsuserRepository; 
            }
        }
 
        public IBotRecipientRepository BotRecipientRepository
        {
            get
            {
                if (this._botrecipientRepository == null)
                {
                    this._botrecipientRepository = new BotRecipientRepository(this.context);
                }
                
                return this._botrecipientRepository; 
            }
        }
 
        public IBotPeriodHourRepository BotPeriodHourRepository
        {
            get
            {
                if (this._botperiodhourRepository == null)
                {
                    this._botperiodhourRepository = new BotPeriodHourRepository(this.context);
                }
                
                return this._botperiodhourRepository; 
            }
        }
 
        public IBotsOperationRepository BotsOperationRepository
        {
            get
            {
                if (this._botsoperationRepository == null)
                {
                    this._botsoperationRepository = new BotsOperationRepository(this.context);
                }
                
                return this._botsoperationRepository; 
            }
        }
 
        public IOperationRepository OperationRepository
        {
            get
            {
                if (this._operationRepository == null)
                {
                    this._operationRepository = new OperationRepository(this.context);
                }
                
                return this._operationRepository; 
            }
        }
 
        public IBOTRepository BOTRepository
        {
            get
            {
                if (this._botRepository == null)
                {
                    this._botRepository = new BOTRepository(this.context);
                }
                
                return this._botRepository; 
            }
        }
 
        public IUserRepository UserRepository
        {
            get
            {
                if (this._userRepository == null)
                {
                    this._userRepository = new UserRepository(this.context);
                }
                
                return this._userRepository; 
            }
        }
 
        public IAuditedOperationRepository AuditedOperationRepository
        {
            get
            {
                if (this._auditedoperationRepository == null)
                {
                    this._auditedoperationRepository = new AuditedOperationRepository(this.context);
                }
                
                return this._auditedoperationRepository; 
            }
        }

        #endregion

        #region Public Methods
        
        public void Commit()
        {
             this.context.SaveChanges();
        }
        
        public void Dispose()
        {
             this.Dispose(true);
             GC.SuppressFinalize(this);
        }
		
        #endregion
        
        #region Proptected Virtual Methods
        
        protected virtual void Dispose(bool disposing)
        {
             if (!this.disposed)
             {
                 if (disposing)
                 {
                     this.context.Dispose();
                 }
             }
            
             this.disposed = true;
         }
        
        #endregion
    }
}
